<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class User_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 通过学号和密码查找用户
	 */
	public function find_by_sno_password($sno,$password){
		$this->db->where('sno', $sno);
		$this->db->where('password', $password);
		$query = $this->db->get('t_user');
		return $query->row_array();
	}
	/*
	 * 根据ID得到用户信息
	 */
	public function load($id){//
		$this->db->select('t_user.id,t_user.username,t_user.password,t_user.phone,t_user.pname,t_user.sname,t_user.class,t_user.sno,t_user.balance,t_user.regist_time,t_user.age,t_user.gender,t_user.order_state,t_class.schoolid as school,t_class.gradeid as grade');
		$this->db->from('t_user');
		$this->db->join('t_class', 't_user.class = t_class.id','left');
		$this->db->where("t_user.id", $id);
		$query = $this->db->get();
		return  $query->row_array();
	}
	/*
	 * 更新用户信息
	 */
	public function update($id,$user){
		$this->db->where('id', $id);
		$this->db->update('t_user', $user);
		return $this->db->affected_rows()>0;
	}
	
	
	/*
	 * 根据用户名和密码得到用户
	 */
	public function get_user($username, $password) {
		$query = $this->db->get_where ( 't_user', array (
				'username' => $username,
				'password' => $password 
		) );
		
		if ($query->num_rows () > 0) {
			return $query->row_array ();
		}
		return array ();
	}
	/*
	 * 通过用户名查找用户
	 */
	public function find_by_username($username){
		$this->db->where('username', $username);
		$query = $this->db->get('t_user');
		return $query->row_array();
	}
	/*
	 * 通过班级、姓名和学号查找用户
	 */
	public function find_by_class_name($class,$name){
		$this->db->where('class', $class);
		$this->db->where('sname', $name);
		$query = $this->db->get('t_user');
		return $query->row_array();
	}
	/*
	 * 保存用户信息
	 */
	public function create($user){
		$this->db->insert('t_user', $user);
		return $this->db->insert_id();
	}
}
